package com.hrb.douban.mview;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.hrb.douban.R;
import com.hrb.douban.utils.SizeConvertUtil;

/**
 * 收藏点赞按钮
 */
public class CollectBtn extends View {
    private Bitmap mNormalStar;
    private Bitmap mSelectStar;
    private int mNormalColor=Color.GRAY;
    private int mSelectColor=Color.RED;
    private int mLetterSize=16;
    private String mNormalText="收藏";
    private String mSelectText="已收藏";
    private boolean mState=false;

    private Paint mStarPaint;
    private Paint mTextPaint;
    private Paint mTextPaintSelect;
    private CollectBtnChangeListener mCollectBtnChangeListener;

    public CollectBtn(Context context) {
        this(context,null);
    }

    public CollectBtn(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs,0);
    }

    public CollectBtn(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray td=context.obtainStyledAttributes(attrs, R.styleable.CollectBtn);
        int normalRes= td.getResourceId(R.styleable.CollectBtn_normalStar,0);
        mNormalStar = BitmapFactory.decodeResource(getResources(), normalRes);
        int selectRes= td.getResourceId(R.styleable.CollectBtn_selectStar,0);
        mSelectStar= BitmapFactory.decodeResource(getResources(),selectRes);

        mState=td.getBoolean(R.styleable.CollectBtn_state,mState);
        mNormalColor =td.getColor(R.styleable.CollectBtn_normalColor,mNormalColor);
        mSelectColor=td.getColor(R.styleable.CollectBtn_selectColor,mSelectColor);
        mLetterSize=td.getDimensionPixelOffset(R.styleable.CollectBtn_letterSize,SizeConvertUtil.dpTopx(context,mLetterSize));
        td.recycle();

        mStarPaint=new Paint();


        /*未选中的文字*/
        mTextPaint=new Paint();
        mTextPaint.setAntiAlias(true);
        mTextPaint.setColor(mNormalColor);
        mTextPaint.setTextSize(mLetterSize);

        /*选中文字*/
        mTextPaintSelect=new Paint();
        mTextPaintSelect.setAntiAlias(true);
        mTextPaintSelect.setTextSize(mLetterSize);
        mTextPaintSelect.setColor(mSelectColor);

    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        int oneW=(int)mTextPaint.measureText("已");
        getWidth();
        //int width=MeasureSpec.getSize(widthMeasureSpec);;

        int width=getPaddingLeft()+getPaddingRight()+(mState?3*oneW+mSelectStar.getWidth():2*oneW+mNormalStar.getWidth())+20;
        int height=MeasureSpec.getSize(heightMeasureSpec);

        setMeasuredDimension(width,mSelectStar.getHeight()+20);
    }

    @Override
    protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
        super.onLayout(changed, left, top, right, bottom);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        /*定义外边框*/
       /* RectF backGroundRectF = new RectF(0, 0, canvas.getWidth(), canvas.getHeight());
        Paint backGroundPaint = new Paint();
        backGroundPaint.setAlpha(255);
        backGroundPaint.setColor(Color.BLACK);
        canvas.drawRoundRect(backGroundRectF, 10, 10, backGroundPaint);*/
        Rect rect= new Rect();
        Paint.FontMetricsInt fontMetricsInt=mTextPaint.getFontMetricsInt();
        float dy=(fontMetricsInt.bottom-fontMetricsInt.top)/2-fontMetricsInt.bottom;
        float baseline =getHeight()/2+dy;

        if(mState){
            canvas.drawBitmap(mSelectStar,6,10,mStarPaint);
            mTextPaintSelect.getTextBounds(mSelectText,0,mSelectText.length(),rect);
            canvas.drawText(mSelectText,0,mSelectText.length(),mSelectStar.getWidth()+12,baseline,mTextPaintSelect);
        }else {
            canvas.drawBitmap(mNormalStar,6,5,mStarPaint);
            mTextPaint.getTextBounds(mNormalText,0,mNormalText.length(),rect);
            //要写得文字、从字符第几个开始，到第几个结束，开始横坐标
            canvas.drawText(mNormalText,0,mNormalText.length(),mNormalStar.getWidth()+12,baseline,mTextPaint);
        }
    }

    @Override
    public boolean onTouchEvent(MotionEvent event) {
        switch (event.getAction()){
            case MotionEvent.ACTION_DOWN:
                mState=!mState;
                requestLayout();
                if(mCollectBtnChangeListener!=null){
                    mCollectBtnChangeListener.onClick(mState);
                }
                invalidate();
                break;
        }
        return true;
    }

    public void setmCollectBtnChangeListener(CollectBtnChangeListener mCollectBtnChangeListener) {
        this.mCollectBtnChangeListener = mCollectBtnChangeListener;
    }

    public void setmState(boolean mState) {
        this.mState = mState;
        requestLayout();
        invalidate();
    }

    public interface CollectBtnChangeListener{
        void onClick(boolean flag);
    }
}
